package com.jie.other.ui.widget

import androidx.compose.foundation.layout.RowScope
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp

/**
 * 封装通用的TopAppBar
 */
@Composable
fun CommonTopAppBar(
    title: String,
    actions: @Composable RowScope.() -> Unit = {},
    isShowBack: Boolean = true,
    backClick: () -> Unit = {}
) {
    TopAppBar(
        navigationIcon = {
            if (isShowBack) {
                IconButton(
                    onClick = backClick
                ) {
                    Icon(
                        imageVector = Icons.Default.ArrowBack,
                        tint = Color.White,
                        contentDescription = "",
                    )
                }
            }
        },
        title = {
            Text(
                text = title,
                color = Color.White,
                fontSize = 18.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
        },
        actions = actions
    )
}

@Preview
@Composable
fun PreviewShortTitle() {
    CommonTopAppBar(
        title = "这是测试文本",
        isShowBack = true,
    )
}

@Preview
@Composable
fun PreviewLongTitle() {
    CommonTopAppBar(
        title = "这是测试文本这是测试文本这是测试文本这是测试文本",
        isShowBack = true,
    )
}